<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="settlementFormRules" :model="settlementForm" label-position="left" label-width="140px">
      <el-form-item label="编码" prop="code">
        <el-input v-model="settlementForm.code" />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="settlementForm.title" />
      </el-form-item>
      <el-form-item label="费率" prop="rate">
        <el-input v-model="settlementForm.rate" />
      </el-form-item>
      <el-form-item label="配置" prop="rules">
        <JsonEditor v-model="settlementForm.rules"></JsonEditor>
      </el-form-item>
      <el-form-item label="通知地址" prop="noticeUri">
        <el-input v-model="settlementForm.noticeUri" />
      </el-form-item>
      <el-form-item label="图标">
        <Upload v-model="settlementForm.avatar" />
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="settlementForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        确认
      </el-button>
      <el-button v-if="settlementForm.status.value === 5" type="danger" @click="handleDelete(settlementForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveSettlement, removeSettlementById, getSettlement } from '@/api/market/settlement'
import waves from '@/directive/waves' // waves directive
import Upload from '@/components/Upload/SingleImage' // secondary package based on el-pagination
import JsonEditor from '@/components/JsonEditor' // secondary package based on el-pagination
import { } from '@/utils'

export default {
  name: 'ComplexTable',
  components: { Upload,JsonEditor },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      settlementForm: {
        code: '',
        title: '',
        avatar: '',
        rules:{
          appKey:"",
          appSecret:"",
          noticeUrl:"",
          publicKey:"",
          privateKey:""
        },
        rate: '',
        remark: '',
        status: {
          label: '',
          value: 1
        }
      },
      settlementFormRules: []
    }
  },
  created() {
    let settlementID = this.$route.query && this.$route.query.settlementID
    if (settlementID !== undefined) {
      settlementID = parseInt(settlementID)
      if (!isNaN(settlementID)) {
        this.listLoading = true
        const formData = {
          'settlementID': settlementID
        }
        getSettlement(formData).then(response => {
          if (response.status === 200) {
            if(response.data.rules == null){
              response.data.rules = this.settlementForm.rules;
            }
            this.settlementForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(settlementID)
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'settlement' })
    },
    createData() {
      this.listLoading = true
      var _settlementForm = this.settlementForm
      _settlementForm.status = _settlementForm.status.value
      if (_settlementForm.identity > 0) {
        _settlementForm.settlementID = _settlementForm.identity
      }
      saveSettlement(_settlementForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'settlement' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      const fromdata = {
        'settlementID': row.identity
      }
      removeSettlementById(fromdata).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'settlement' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
